package com.jason.weibosender.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import weibo4j.Friendships;
import weibo4j.Timeline;
import weibo4j.model.Status;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;

public class ClientFrame extends JFrame implements ActionListener{
	JTextArea textArea;
	JScrollPane scrollContent;
	JPanel buttonPanel;
	JButton buttonFollows;
	JButton button;
	String accessToken;
	long size;
	public ClientFrame(String accessToken, long size){
		this.accessToken = accessToken;
		this.size = size;
		initComponents();
	}
	private void initComponents(){
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		scrollContent = new JScrollPane(textArea);
		scrollContent.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollContent.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		buttonFollows = new JButton("粉丝(" + size + ")");
		button = new JButton("发送");
		buttonFollows.addActionListener(this);
		button.addActionListener(this);
		buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.add(buttonFollows);
		buttonPanel.add(button);
		this.setLayout(new BorderLayout());
		this.add(scrollContent, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		scrollContent.setSize(500, 200);
		this.setSize(500, 200);
		this.setLocationRelativeTo(null);
		ImageIcon imageIcon = new ImageIcon(ClientFrame.class.getResource("weibo.jpg"));
		this.setIconImage(imageIcon.getImage());
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private byte[] readInputStream(InputStream ips){
		if(ips == null){
			return null;
		}
		List<Byte> result = new ArrayList<Byte>();
		try {
			byte tempByte;
			while((tempByte = (byte)ips.read()) != -1){
				result.add(tempByte);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] resultByte = new byte[result.size()];
		for(int i = 0; i < result.size(); i++){
			resultByte[i] = result.get(i);
		}
		return resultByte;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object eventPoster = e.getSource();
		if(buttonFollows == eventPoster){
			Friendships friendShips = new Friendships();
			friendShips.client.setToken(accessToken);
			try {
				UserWapper users = friendShips.getFollowersByName("袖珍小说家");
				List<User> userList = users.getUsers();
				for(User user : userList){
					System.out.println(user.getScreenName());
				}
			} catch (WeiboException e1) {
				e1.printStackTrace();
			}
		} else if(button == eventPoster){
			Timeline tm = new Timeline();
			tm.client.setToken(accessToken);
			try {
				Status status = tm.UpdateStatus(textArea.getText());
				JOptionPane.showMessageDialog(this, "发布！");
			} catch (WeiboException e1) {
				e1.printStackTrace();
			}
		}
	}
}
